package com.jeesite.modules.app.examinate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.service.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.modules.app.mockexam.special.user.entity.TmockexamSpecialUser;
import com.jeesite.modules.app.mockexam.special.user.service.TmockexamSpecialUserService;
import com.jeesite.modules.app.mockexam.theory.entity.AppExaminateTheory;
import com.jeesite.modules.app.mockexam.theory.service.AppExaminateTheoryService;
import com.jeesite.modules.app.mockexam.theory.user.entity.TmockexamTheoryUser;
import com.jeesite.modules.app.mockexam.theory.user.service.TmockexamTheoryUserService;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.zx.barbecue.signup.service.TbarbecueSignupService;
import com.jeesite.modules.zx.course.entity.Tcourses;
import com.jeesite.modules.zx.course.exercises.user.entity.TcourseExercisesUser;
import com.jeesite.modules.zx.course.exercises.user.service.TcourseExercisesUserService;
import com.jeesite.modules.zx.course.service.TcoursesService;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/app/customer/examinate/")
public class CustomerExaminateRecordeController {
	@Autowired
	private TcourseExercisesUserService tcourseExercisesUserService;
	@Autowired
	private TmockexamSpecialUserService tmockexamSpecialUserService;
	@Autowired
	private TcoursesService tcoursesService;
	@Autowired
	private TmockexamTheoryUserService tmockexamTheoryUserService;
	@Autowired
	private TbarbecueSignupService tbarbecueSignupService;
	@Autowired
	private AppExaminateTheoryService appExaminateTheoryService;

	@Autowired
	private DictDataService dictDataService;


	@RequestMapping(value = "findPage")
	public void findPage(HttpServletRequest request,HttpServletResponse response){
		String userId=request.getParameter("userId");
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			return;
		}
		//1.查询客户练习
		TcourseExercisesUser teu=new TcourseExercisesUser();
		teu.setCreateBy(userId);
		teu.setPage(new Page<>(request, response));
		Page<TcourseExercisesUser> findPage = tcourseExercisesUserService.findPage(teu);
		for(TcourseExercisesUser course:findPage.getList()){
	        Tcourses tcourses=tcoursesService.get(course.getId());
	        course.setTcourses(tcourses);
		}
		JSONObject teuJson=JSONObject.parseObject(JsonMapper.toJson(findPage));
		//2.查询专项练习
		//3.查询理论模拟考试
		TmockexamTheoryUser ttu=new TmockexamTheoryUser();
		ttu.setUserId(userId);
		ttu.setPage(new Page<>(request, response));
		Page<TmockexamTheoryUser> findPage3 = tmockexamTheoryUserService.findPage(ttu);
		for(TmockexamTheoryUser theory:findPage3.getList()){			
		  AppExaminateTheory appExaminateTheory=appExaminateTheoryService.get(theory.getTheoryId());
		  theory.setTheory(appExaminateTheory);
		}
		JSONObject ttuJson=JSONObject.parseObject(JsonMapper.toJson(findPage3));
		JSONObject json =new JSONObject();
		json.put("teuJson", teuJson);
		json.put("ttuJson", ttuJson);
		AppController.renderSuccessObject(response, json, MessageCode.SUCCESS_ACK);
	}

	/**
	 * 获取专项类型的类别,来填充页面
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "getKnowingType")
	public void getKnowingType(HttpServletRequest request,HttpServletResponse response){
		String dictType=request.getParameter("dictType");
		if(StringUtils.isBlank(dictType)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "无专项类型");
			return  ;
		}
		DictData dictData = new DictData();
		dictData.setDictType(dictType);
		List<DictData> list = dictDataService.findList(dictData);
		List<Map<String,Object>> listMap = new ArrayList<>();
		if (list.size()>0){
			list.forEach(item->{
				Map<String,Object> map = new HashMap<>();
				map.put("dictLabel",item.getDictLabel());
				map.put("dictValue",item.getDictValue());
				map.put("dictType",item.getDictType());
				listMap.add(map);
			});
		}
		JSONArray parseObject = JSONObject.parseArray(JsonMapper.toJson(listMap));
		AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
	}

}
